package com.lc.hongtai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.activity.AdvertisingActivity;
import com.lc.hongtai.activity.MsgDetailActivity;
import com.lc.hongtai.conn.JsonMsglist;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 4/20/2016.
 */
public class NewsAndMsgAdapter extends RecyclerView.Adapter<NewsAndMsgAdapter.ViewHolder> {


    private Context context;
    private int flag = 0;
    private List<JsonMsglist.Info.ListContent> list;

    public NewsAndMsgAdapter(Context context, int flag, List<JsonMsglist.Info.ListContent> list) {
        this.context = context;
        this.flag = flag;
        this.list = list;

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_01)
        TextView tv01;
        /*  @Bind(R.id.iv_01)
          ImageView iv01;*/
        @Bind(R.id.tv_02)
        TextView tv02;
        @Bind(R.id.view_lines)
        View view_lines;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            MyApplication.scaleScreenHelper.loadView((ViewGroup) view);

        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_15_item_view, null);

        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(view);

        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (flag == 0) {
            // holder.tv01.setText("收件时间：" + list.get(position).send_time);
            holder.tv02.setText(list.get(position).title);
            String str_time = list.get(position).send_time;

            if (list.size() - 1 == position) {

                holder.view_lines.setVisibility(View.GONE);
            } else {

                holder.view_lines.setVisibility(View.VISIBLE);

            }


            DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = format1.parse(str_time);
            } catch (Exception e) {
                e.getMessage();
            }

            date = new Date();
            str_time = format1.format(date);
            holder.tv01.setText(str_time);


        } else if (flag == 1) {
            // holder.iv01.setVisibility(View.GONE);
            holder.tv01.setText(list.get(position).send_time);
            holder.tv02.setText(list.get(position).title);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("id", list.get(position).id);
                if (flag == 0) {
                    intent.setClass(context, MsgDetailActivity.class);
                } else if (flag == 1) {
                    intent.putExtra("title", "新闻公告详情");
                    // http://vhost119.zihaistar.com/api/index/invest_detail?id=3&show_type=borrow_info&access_token=4d2685fae1729bfa0d9ac59c939996a0
//                    intent.putExtra("url","https://www.haiying365.com/api/index/article_content?id="+list.get(position).id+"&show_type=article");
// @HttpServer("http://vhost106.zihaistar.com/api/")
                    intent.putExtra("url", "http://vhost106.zihaistar.com/api/index/article_content?id=" + list.get(position).id + "&show_type=article");
                    intent.setClass(context, AdvertisingActivity.class);
                }
                context.startActivity(intent);
            }
        });

    }

    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return list.size();
    }
}
